<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="list" label="代码">
        <el-input size="small" v-model="dataForm.list" placeholder="代码" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
      <el-form-item prop="listname" label="代码名称" >
        <el-input size="small" v-model="dataForm.listname"  placeholder="代码名称"></el-input>
      </el-form-item>
	  <el-form-item prop="listname" label="排序" >
	    <el-input size="small" v-model="dataForm.sort"  placeholder="排序"></el-input>
	  </el-form-item>
	  <el-form-item prop="reserve1" label="备用字段1" >
	    <el-input size="small" v-model="dataForm.reserve1"  placeholder="备用字段1"></el-input>
	  </el-form-item>
	  <el-form-item prop="reserve2" label="备用字段2" >
	    <el-input size="small" v-model="dataForm.reserve2"  placeholder="备用字段2"></el-input>
	  </el-form-item>
	  <el-form-item prop="reserve3" label="备用字段3" >
	    <el-input size="small" v-model="dataForm.reserve3"  placeholder="备用字段3"></el-input>
	  </el-form-item>
	  <el-form-item prop="reserve4" label="备用字段4" >
	    <el-input size="small" v-model="dataForm.reserve4"  placeholder="备用字段4"></el-input>
	  </el-form-item>
	  <el-form-item prop="reserve5" label="备用字段5" >
	    <el-input size="small" v-model="dataForm.reserve5"  placeholder="备用字段5"></el-input>
	  </el-form-item>
    </el-form>
    <template #footer>
      <el-button size="small" @click="visible = false">取消</el-button>
      <el-button size="small" type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {validEmail, isMobile} from "@/utils/validate";
import {getDeptLists} from "@/network/sys/dept";
import {getCodelistById,putCodelist,postCodelist} from "@/network/lswms/base/Codelist"
import {ElMessage} from 'element-plus'
import {getRoles} from "@/network/sys/role";
import _ from 'lodash'


export default {
  name: "CodeAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    // const deptList = ref([]);
    // const deptListVisible = ref(false);
    // const roleList = ref([]);
    // const roleIdListDefault = ref([]);
    const dataForm = ref({
		id:"",
      code: '',
      list: '',
	  sort: 0 ,
	  listname: '',
	  reserve1: '',
	  reserve2: '',
	  reserve3: '',
	  reserve4: '',
	  reserve5: '',
    });


    const dataRule = reactive({
      list: [
        {required: true, message: '必填项不能为空', trigger: 'blur'}
      ],
      listname: [
        {required: true, message: '必填项不能为空', trigger: 'blur'}
      ]
    })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        // roleIdListDefault.value = [];
		if (dataForm.value.id) {
			getCodelistInfo();
		}
        // Promise.all([
        //   getDeptList(),
        //   getRoleList()
        // ]).then(function () {
        //   if (dataForm.value.id) {
        //     getInfo();
        //   }
        // })
      })
    }

	const getCodelistInfo =()=>{
		getCodelistById(dataForm.value.id).then(function (res) {
		  if (res.code !== 0) {
		    return ElMessage.error(res.msg);
		  }
		  dataForm.value = _.assignIn({}, dataForm.value, res.data);
		}).catch((err) =>{
		});
	}

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }
		
        let qing = ()=> {
          return !dataForm.value.id ?  postCodelist(dataForm.value) : putCodelist(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      // deptList,
      // deptListVisible,
      // roleList,
      // roleIdListDefault,
      dataForm,
      dataRule,
      // validatePassword,
      // validateComfirmPassword,
      // validateEmail,
      // validateMobile,
      init,
      // getDeptList,
      // getRoleList,
      // getInfo,
      // deptListTreeCurrentChangeHandle,
      dataFormSubmitHandle,
      // deptListTree,
      dataFormRef,
	  getCodelistInfo

    }
  },

  beforeCreate: function () {
    self = this;
  },

}
</script>

<style scoped>

</style>